package com.example.springboot.entity;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class EmailInfo {
    /**
     * 想QQ邮箱发送邮件
     *
     * @param formMail
     *   发送人邮箱地址
     * @param descMail
     *   接收人邮箱地址
     * @param subject
     *   邮箱主题
     * @param content
     *   邮箱内容
     * @param files
     *   附件列表
     * @param contentType
     *   内容格式
     * @param password
     *   SMTP密码
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public static void sendQQMail(String formMail, String descMail, String subject, String content, File[] files,
                                  String contentType, String password) throws MessagingException, UnsupportedEncodingException {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(formMail, password);
            }
        });
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(formMail));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(descMail));
            message.setSubject(subject);

            // 是否存在附件
            if (null != files && files.length > 0) {
                MimeMultipart multipart = new MimeMultipart();

                BodyPart contentPart = new MimeBodyPart();
                contentPart.setContent(content, contentType);
                multipart.addBodyPart(contentPart);

                for (File file : files) {
                    MimeBodyPart attachment = new MimeBodyPart();
                    DataHandler dh3 = new DataHandler(new FileDataSource(file));
                    attachment.setDataHandler(dh3);
                    attachment.setFileName(MimeUtility.encodeText(dh3.getName()));
                    multipart.addBodyPart(attachment);
                }
                multipart.setSubType("mixed");

                message.setContent(multipart);
                message.saveChanges();
            }
            // 普通
            else {
                message.setContent(content, contentType);
            }

            Transport transport = session.getTransport();
            transport.connect(formMail, password);
            Transport.send(message);
        } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (NoSuchProviderException e) {
            throw e;
        } catch (MessagingException e) {
            throw e;
        }

    }

    public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
        // 由哪个邮箱发送
        String formMail = "********@qq.com";
        // QQ邮箱>设置>账户 开启POP3/SMTP服务 查看smtp密码
        String smtpPassword = "****************";

        // 发送人邮箱地址
        String descMail = "470947852@qq.com";
        String contentType = "text/html;charset=UTF-8";

        String subject = "测试邮件发送，含附件";
        String content = "test send mail, 这里是中文";
        File[] files = new File[2];
        files[0] = new File("C:/test_1.xls");
        files[1] = new File("C:/test_2.xls");

//        SendMail.sendQQMail(formMail, descMail, subject, content, files, contentType, smtpPassword);
    }
}
